{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "46178e59",
   "metadata": {},
   "source": [
    "# python基础入门9_  Numpy与矩阵\n",
    "NumPy是一个Python编程语言的库，它支持大型多维数组和矩阵，并提供了大量的高级数学函数来操作这些数组。它通常被用作Python科学计算的基础包。\n",
    "\n",
    "## 安装方法：\n",
    "\n",
    "打开anaconda prompt在窗口中输入安装指令：\n",
    "\n",
    "pip install numpy 或者 conda install numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d467ab6",
   "metadata": {},
   "source": [
    "## 导入NumPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "bb6fa17b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入NumPy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1944950a",
   "metadata": {},
   "source": [
    "## 创建一维NumPy数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "1c449275",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建NumPy数组\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "e3cfbf01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1, 2, 3, 4, 5]\n",
    "arr=np.array(lst)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "7aa6acf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "57927007",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "d1fbca66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape#(row，column)->row行，column列;（row,）->row行1列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8206ee0d",
   "metadata": {},
   "source": [
    "## 访问一维NumPy数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "08dd060b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#访问全部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "1ad95e79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "1151858f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "a18d052d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#访问局部（切片）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "14e888cb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4]\n"
     ]
    }
   ],
   "source": [
    "print(arr[2:4]) # 打印数组中索引为2到3的元素————————range(0,10)->0~9————————for(i=0;i<n;i++)————>i：0~n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "17a34b07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 4, 4, 5])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "fe355c9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 4, 5])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "5c4eb7fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#访问单个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "cef2592b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(arr[0]) # 打印数组的第一个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1f24375",
   "metadata": {},
   "source": [
    "## 修改一维NumPy数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "e3a70fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "637d42e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[2]=4#通过index修改数组值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "147735fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 4, 4, 5])"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "df9ef39a",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[:]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "67e3489d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eab5ea4a",
   "metadata": {},
   "source": [
    "## 一维NumPy数组的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "f7ce6dd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[-3 -3 -3]\n",
      "[ 4 10 18]\n",
      "[0.25 0.4  0.5 ]\n"
     ]
    }
   ],
   "source": [
    "#对NumPy数组执行数学运算\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "###数组四则运算\n",
    "print(np.add(arr1, arr2)) # 逐元素相加两个数组\n",
    "print(np.subtract(arr1, arr2)) # 逐元素相减第二个数组从第一个数组中\n",
    "print(np.multiply(arr1, arr2)) # 逐元素相乘两个数组\n",
    "print(np.divide(arr1, arr2)) # 将第一个数组除以第二个数组逐元素相除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "556ab3de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         1.41421356 1.73205081 2.         2.23606798]\n"
     ]
    }
   ],
   "source": [
    "#将函数应用于NumPy数组的每个元素\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "print(np.sqrt(arr)) # 将平方根函数应用于数组的每个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "5390f3b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维NumPy数组逻辑运算\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "0c787c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr<10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "af03b895",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True])"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr>3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "4a105f70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5])"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr>4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "19b02308",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr>4].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e417974",
   "metadata": {},
   "source": [
    "## 创建多维NumPy数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "34a570ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr=np.array([1,2,3])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "31628295",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 4])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1,2,4]\n",
    "arr=np.array(lst)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "5e59103f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 =  np.array(\n",
    "    [\n",
    "    [1,2,3],\n",
    "    [1,2,3],\n",
    "    [1,2,3]   \n",
    "    ]\n",
    ")\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "d1143794",
   "metadata": {},
   "outputs": [],
   "source": [
    "r1=[1,2,3]\n",
    "r2=[4,5,6]\n",
    "r3=[7,8,9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "c870b46f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([r1,r2,r3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "f659b5ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "41846204",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fa3d6ef",
   "metadata": {},
   "source": [
    "## 访问多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "074f0b92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "63d62600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#单个访问\n",
    "arr[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "c615d278",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "5f24c797",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多个访问\n",
    "arr[:,:]#所有行，所有列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "2efe48d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [4],\n",
       "       [7]])"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:,:1]#全部行，1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "bcdc2f48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [5, 6],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:,1:3]#全部行，下标1到2列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "b56dc363",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:1,:]#0到0行，全部列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "8b875d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0:2,:]#前两行，全部列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "5180b236",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1:3,1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb8cd78",
   "metadata": {},
   "source": [
    "## 修改多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "ec513a77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "50545dee",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[1][1]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "86587ddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 0, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "91fe1e07",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[:]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "c9c081fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2d62547",
   "metadata": {},
   "source": [
    "## 多维数组计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "83b9b1b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 =  np.array(\n",
    "    [\n",
    "    [1,2,3],\n",
    "    [1,2,3],\n",
    "    [1,2,3]   \n",
    "    ]\n",
    ")\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "fbe4cfbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 1, 3],\n",
       "       [3, 3, 1]])"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 =  np.array(\n",
    "    [\n",
    "    [1,2,3],\n",
    "    [2,1,3],\n",
    "    [3,3,1]   \n",
    "    ]\n",
    ")\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "id": "eb744f45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4 6]\n",
      " [3 3 6]\n",
      " [4 5 4]]\n",
      "[[ 0  0  0]\n",
      " [-1  1  0]\n",
      " [-2 -1  2]]\n",
      "[[1 4 9]\n",
      " [2 2 9]\n",
      " [3 6 3]]\n",
      "[[1.         1.         1.        ]\n",
      " [0.5        2.         1.        ]\n",
      " [0.33333333 0.66666667 3.        ]]\n"
     ]
    }
   ],
   "source": [
    "#数组四则运算\n",
    "print(np.add(arr1, arr2)) # 逐元素相加两个数组\n",
    "print(np.subtract(arr1, arr2)) # 逐元素相减第二个数组从第一个数组中\n",
    "print(np.multiply(arr1, arr2)) # 逐元素相乘两个数组\n",
    "print(np.divide(arr1, arr2)) # 将第一个数组除以第二个数组逐元素相除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "2077f493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1<10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "21523808",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([], dtype=int32)"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[arr1>10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "fde9bc9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 1, 2, 3, 1, 2, 3])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[arr1<4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "a9a10d81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9,)"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[arr1<4].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "782f164a",
   "metadata": {},
   "source": [
    "## 随机生成矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "7f4ccbbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#numpy.arrange函数的作用是生成带起点和终点的特定步长的排列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "03157baa",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(1,10)#类似range(0,1)——>序列；arange——>np.array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "af25553c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "id": "71f593b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(3,3)#矩阵形状重构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "e0c4f84e",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(1,100,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "0551f862",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "27a45779",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1, 11, 21, 31, 41],\n",
       "       [51, 61, 71, 81, 91]])"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "11a7d16d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#ones函数-全“1”矩阵\n",
    "arr = np.ones((3,3),dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "7cf24ee9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "0f90364f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.ones((2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "ae80c5bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "ac4b5444",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.23791094, 0.42201699, 0.92872313],\n",
       "       [0.22762039, 0.65436486, 0.40219533],\n",
       "       [0.65199065, 0.96708928, 0.45066071]])"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#rand函数-随机均匀分布矩阵 \n",
    "\n",
    "\n",
    "arr = np.random.rand(3,3)#生成3行3列的0到1直接的随机矩阵\n",
    "\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "ca556ad9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.06805542,  1.78701503, -0.84712042],\n",
       "       [ 0.03485866,  0.14285574,  1.32366684],\n",
       "       [ 0.60998742,  0.55044416, -1.23429433]])"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#randn函数-随机正态分布矩阵 \n",
    "\n",
    "arr = np.random.randn(3,3)#生成3行3列的-1到1直接的随机矩阵\n",
    "\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "id": "090571ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# randint函数-随机生成整数矩阵\n",
    "#randint(low, high=None, size=None, dtype=int)\n",
    "\n",
    "arr = np.random.randint(0,10,20).reshape(4,5)#生成4行5列的每个元素值是0到10之间的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "737c5e61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 1, 1, 3, 9],\n",
       "       [0, 3, 2, 7, 2],\n",
       "       [1, 3, 6, 1, 8],\n",
       "       [7, 0, 2, 3, 3]])"
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "dc6f28cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#random_sample函数-随机生成浮点数矩阵 0~1\n",
    "\n",
    "arr = np.random.random_sample((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "70aa883f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.20873653, 0.69745683, 0.72245158, 0.20350512],\n",
       "       [0.47736678, 0.41727177, 0.92933057, 0.09086499],\n",
       "       [0.2390433 , 0.62220952, 0.95407964, 0.62158511]])"
      ]
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68e6c92a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
